# encoding:utf-8
import asyncio

import openpyxl
import time

import requests
import json

from openpyxl import Workbook
from pyppeteer.launcher import launch
from pyppeteer import launcher




# 主函数
from init_chromium import chromium_init


async def main():
    # "创建一个浏览器实例 browser 对象"
    if '--enable-automation' in launcher.AUTOMATION_ARGS:
        launcher.AUTOMATION_ARGS.remove('--enable-automation')
    browser = await launch({
        'headless': False,
        'dumpio': True,
        'args': [
            '--no-sandbox',  # 禁止沙箱模式
            '--no-default-browser-check',  # 不检查默认浏览器
            '--disable-extensions',
            '--hide-scrollbars',
            '--disable-bundled-ppapi-flash',
            '--mute-audio',
            '--disable-setuid-sandbox',
            '--disable-gpu',
            "--window-size=1500,900",
            "--disable-infobars"  # 禁止提示 浏览器被驱动的提示信息
        ],
    })
    page = await browser.newPage()  # "通过 Browser 对象创建页面 Page 对象"
    await page.setUserAgent(
        'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
    await page.setViewport({"width": 1920, "height": 1080})  # 改变 页面大小

    url_login = "https://dmp.taobao.com/login.html?mxredirectUrl=%2Fhome%2Findex"
    await page.goto(url_login)
    await page.waitForNavigation({'timeout':1200000})
    cookies = await page.cookies()
    with open('cookies.txt', 'w') as f:
        f.write(json.dumps(cookies))

    cookies_str = ''
    for i in cookies:
        cookies_str += ('%s=%s; ' % (i['name'], i['value']))

    # print(cookies_str)

    csrf_data = get_csrfId(cookies_str)
    csrfId = csrf_data["csrfId"]
    await page.close()
    await browser.close()

    xlsxData = getLavel(csrfId,cookies_str)
    xlsxSave(xlsxPath, xlsxData)


def get_csrfId(cookie):
    """
    获取csrfId和店铺名称
    :param headers:
    :return:
    """
    headers = {
        "cookie": cookie,
        "referer": "https://dmp.taobao.com/index_new.html",
        "upgrade-insecure-requests": "1",
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36",
        "x-requested-with": "XMLHttpRequest",
    }
    url = "https://dmp.taobao.com/api_2/login/loginuserinfo"
    response = requests.get(url, headers=headers).text
    response = json.loads(response)
    csrfId = response["data"]["csrfId"]
    shop_name = response["data"]["empowers"][0]["name"]
    return {"code": 200, "csrfId": csrfId, "shop_name": shop_name}



def ran():
    """
    启动函数
    :return:
    """
    chromium_init()
    loop = asyncio.get_event_loop()  # 创建一个事件循环
    loop.run_until_complete(main())  # 将协程加入到事件循环loop
    loop.close()


def initXlsx(xlsxPath):
    book = openpyxl.Workbook()  # 新建工作簿
    sheet = book.active
    title_list2 = ['一级', '二级','三级','标签描述']
    for i in range(len(title_list2)):
        sheet.cell(1, i + 1, title_list2[i])  # 行，列，值 这里是从1开始计数的
    book.save(xlsxPath)
    book.close()


def xlsxSave(xlsxPath, rowDataList):
    data = openpyxl.load_workbook(xlsxPath)
    table = data['Sheet']
    max_row = table.max_row  # 获得行数
    for rowData in rowDataList:
        for index in range(len(rowData)):
            table.cell(max_row+1, index+1).value = rowData[index]
        max_row+=1

    data.save(xlsxPath)


def getLavel(csrfId,cookie):
    params = (
        ('r', 'mx_97'),
        ('csrfId', csrfId),
        ('groupType', '7'),
    )
    HEADER['cookie'] = cookie
    response = requests.get('https://dmp.taobao.com/api_2/tag/group', headers=HEADER, params=params)
    with open('label.json', 'wb') as f:
        f.write(response.content)

    xlsxData = []
    labelList = json.loads(response.text)['data']['groups']
    print(len(labelList))
    for label1 in labelList:
        if label1['parentId'] == 0:  # 一级
            # print(label1['tagGroupName'])
            for label2 in labelList:
                if label2['parentId'] == label1['id']:  # 2
                    # print(label1['tagGroupName'], label2['tagGroupName'])
                    for tag in label2['tags']:
                        tagDesc = json.loads(tag['tagDesc'])['help']
                        print(label1['tagGroupName'], label2['tagGroupName'], tag['tagName'], tagDesc)
                        xlsxData.append([label1['tagGroupName'], label2['tagGroupName'], tag['tagName'], tagDesc])
                    for label3 in labelList:
                        if label3['parentId'] == label2['id']:  # 2
                            # print(label1['tagGroupName'], label2['tagGroupName'], label3['tagGroupName'])
                            for tag2 in label3['tags']:
                                tagDesc2 = json.loads(tag2['tagDesc'])['help']
                                print(label1['tagGroupName'], label2['tagGroupName'], tag2['tagName'], tagDesc2)
                                xlsxData.append(
                                    [label1['tagGroupName'], label2['tagGroupName'], tag2['tagName'], tagDesc2])

    return xlsxData




if __name__ == '__main__':
    xlsxPath = './达摩盘全部标签.xlsx'
    initXlsx(xlsxPath)
    # with open('label.json', 'rb') as f:
    #     labelList = json.loads(f.read().decode())['data']['groups']
    # xlsxData = []
    # for label1 in labelList:
    #     if label1['parentId'] == 0:  # 一级
    #         # print(label1['tagGroupName'])
    #         for label2 in labelList:
    #             if label2['parentId'] == label1['id']:  # 2
    #                 # print(label1['tagGroupName'], label2['tagGroupName'])
    #                 for tag in label2['tags']:
    #                     tagDesc = json.loads(tag['tagDesc'])['help']
    #                     print(label1['tagGroupName'], label2['tagGroupName'], tag['tagName'], tagDesc)
    #                     xlsxData.append([label1['tagGroupName'], label2['tagGroupName'], tag['tagName'], tagDesc])
    #                 for label3 in labelList:
    #                     if label3['parentId'] == label2['id']:  # 2
    #                         # print(label1['tagGroupName'], label2['tagGroupName'], label3['tagGroupName'])
    #                         for tag2 in label3['tags']:
    #                             tagDesc2 = json.loads(tag2['tagDesc'])['help']
    #                             print(label1['tagGroupName'], label2['tagGroupName'],  tag2['tagName'], tagDesc2)
    #                             xlsxData.append([label1['tagGroupName'], label2['tagGroupName'],tag2['tagName'], tagDesc2])


    # print(len(xlsxData))
    # xlsxSave(xlsxPath, xlsxData)

    HEADER = {
        'authority': 'dmp.taobao.com',
        'accept': 'application/json, text/javascript, */*; q=0.01',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36',
        'x-requested-with': 'XMLHttpRequest',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://dmp.taobao.com/index_new.html',
        'accept-language': 'zh-CN,zh;q=0.9',
        # 'cookie': 't=ecc62c55fb4466178326987d09c1ef39; cookie2=1f2b0e7446f8718397985d92d8da9fee; _tb_token_=e3b333e1e384a; _samesite_flag_=true; tfstk=clrOBOaeh6fGHkDLUPQnlU9hXr6lZvfxOdGDDtJwVZiD0YtAiIEu2zqR5xOtv9C..; sgcookie=ESwvcOrH7oCISd5rbQDNa; unb=2200537235551; sn=%E5%BC%A0%E5%BA%86%E5%8D%87777%3A%E8%80%81%E6%A2%81; uc1=cookie14=UoTV7gOtqPLjOQ%3D%3D&cookie21=VT5L2FSpdiBh; csg=4447e002; _cc_=UtASsssmfA%3D%3D; skt=55ecf67bc9fba994; cna=9xduF1I3oEECAXPjwP/UC41j; JSESSIONID=717C8418C5EC495B2B65647416D1C03E; l=eBS9dAhVQ88IPvqWmOfwourza77OSIRAguPzaNbMiOCP_z5J5e8OWZxk1QLvC3GVh6jHR35QyF6TBeYBqIvioBVTyBpNHtHmn; isg=BJ2dpE9zn9tl13vxJpdr6741rHmXutEMKxfjl19i9_QjFr1IJg2S3YeQQgoQ1unE',
    }

    ran()
